<?php

/*

f()->users([$login]);
f()->current_user();
f()->find_user_by_name([$login]);
f()->registration()->fields;
f()->registration()->_f_fields_;

Properties:
$user->login;

	$user->name;
	$user->title;
	$user->first_name;
	$user->middle_name;
	$user->last_name;

Functions:
$user->check_password($password);
$user->save();

*/

class _F_MODEL_USER_ extends _F_EXTENSION_DATA_MODEL_ {
	
	public $id;
	public $password;
	public $salt;
	public $user_state;
	public $first_name;
	public $last_name;
	public $gender;
	public $date_of_birth;
	public $address;
	public $city;
	public $state;
	public $country;
	public $zip;
	public $email;
	public $phone;
	public $mobile;
	
	public function __construct() {
		$this->_f_fields_ = array('id','password','salt','first_name',
			'last_name','gender','date_of_birth','address','city',
			'state','country','zip','email','phone','mobile');
		$this->_f_pkey_field_ = 'id';
		// $this->_f_autoincrement_ = true;
		$this->_f_table_ = 'user';
	}

	public function change_password($password) {
		$this->salt = substr(md5(uniqid(rand(),true)),0,32);
		$this->password = md5($this->salt.$password);		
	}
	
	public function check_password($password) {
		return $this->password == md5($this->salt.$password);		
	}
	
	public function _f_get_name_() {
		return trim($this->first_name.' '.$this->last_name);
	}

	public function _f_get_gender_label_() {
		return t($this->gender);
	}

	public function _f_get_full_address_() {
		$address = trim($this->address);
		$city = trim($this->city);
		$zip = trim($this->zip);
		if ($address != '' &&  $city != '') $address.= ', ';
		$address .= $city;
		if ($address != '' &&  $zip != '') $address.= ', ';
		$address .= $zip;
		return $address;
	}
	
}
